<?php

namespace App\Admin\Actions\Waybill;

use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Rap2hpoutre\FastExcel\FastExcel;
use Excel;
use App\Imports\WaybillImport;

class ImportWaybill extends Action
{
    protected $selector = '.import-waybill';

    public function handle(Request $request)
    {
        set_time_limit(0);

        $file = $request->file('file');

        $mimeTye = $file->getMimeType();

        $file_name = $file->getRealPath();
         // dd($mimeTye, $file_name);

        // $support_types = ['application/vnd.ms-excel'];

        try
        {
            Excel::import(new WaybillImport, $file);
        }
        catch (Exception $e)
        {
            return $this->response()->error($e->getMessage())->refresh();
        }

        return $this->response()->success('导入完成')->refresh();
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default import-waybill">导入运单</a>
HTML;
    }

    public function form()
    {
        $this->file('file', '请选择要导入文件')->help('导入运单');
    }
}